/*
 @brief Soft IIC for STM32F10x, tested on stm32f103c6t6a.
 
 Stone at home, Feb 2023
 */

#ifndef SOFTIIC_H__
#define SOFTIIC_H__

#include "time.h"
#include "SoftIIC_hardware.h"


#define SOFT_IIC_ACK_OK  0
#define SOFT_IIC_ACK_ERR 1

#define SOFT_IIC_DIR_WR 0  // IIC direction write
#define SOFT_IIC_DIR_RD 1  // IIC direction read


// ===== Signal timing, in micro seconds. =====
#define SOFT_IIC_TIM_STR_STP_DLY 2      // delay of the signal for start/stop condition
#define SOFT_IIC_TIM_CLK_LOW  2         // clock pulse width low
#define SOFT_IIC_TIM_CLK_HIGH 2         // clock pulse width high
#define SOFT_IIC_TIM_DAT_DLY  2         // delay to wait SDA to become stable after changed


// ===== IIC Primitives =====
void SoftIIC_Init(const SoftIIC_HardwareTypeDef * hardware);
void SoftIIC_Start(const SoftIIC_HardwareTypeDef * hardware);
void SoftIIC_Stop(const SoftIIC_HardwareTypeDef * hardware);
void SoftIIC_Ack(const SoftIIC_HardwareTypeDef * hardware);
int SoftIIC_WaitAck(const SoftIIC_HardwareTypeDef * hardware);
void SoftIIC_SendByte(const SoftIIC_HardwareTypeDef * hardware, unsigned char data);
unsigned char SoftIIC_ReadByte(const SoftIIC_HardwareTypeDef * hardware);


// ===== IIC higher level functions =====
void SoftIIC_SendAddr(const SoftIIC_HardwareTypeDef * hardware, unsigned char addr, unsigned char dir);

#endif  // SOFTIIC_H__
